package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台平台管理中心
 * @author aaronmegs
 * @create 2020-02-18 7:32 下午
 */
@Api(tags = "后台平台管理中心")
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin //#跨域注解  CORS  cross origin resource share
public class ManageController {

    @Autowired
    private ManageService manageService;

    // 获取一级分类
    @ApiOperation("获取一级分类集合")
    @GetMapping("getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> category1List = manageService.getCategory1(); // 所有一级分类
        return Result.ok(category1List);
    }

    // 获取二级分类
    @ApiOperation("获取二级分类集合")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id) {
        // 根据一级分类的Id 查询二级分类的集合
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }

    @ApiOperation("获取三级分类集合")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id) {
        // 根据二级分类的Id 查询三级分类的集合
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }

    @ApiOperation("根据一二三级分类Id 获取平台属性及属性值")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    @ApiOperation("保存平台属性及属性值  json格式字符串  接收时对象")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //{page}/{limit}?category3Id=61
    @ApiOperation("获取spu分页列表")
    @GetMapping("{page}/{limit}")
    public Result getSpuPage(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit,
            @RequestParam Long category3Id) {
        // MybatisPlus的分页插件
        IPage spuPage = manageService.getSpuPage(page, limit, category3Id);
        return Result.ok(spuPage);
    }

    @ApiOperation("获取基本销售属性列表")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    @ApiOperation("获取品牌列表")
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> baseTrademarkList = manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    // 获取品牌分页列表
    @ApiOperation("获取品牌分页列表")
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result getTrademarkPage(@PathVariable(name = "page") Integer page,
                                   @PathVariable(name = "limit") Integer limit) {
        IPage trademarkPage = manageService.getTrademarkPage(page, limit);
        return Result.ok(trademarkPage);
    }

    // 保存SPU数据 SPU图片 销售属性 销售熟悉值 四张表
    @ApiOperation("保存SPU数据")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    // 获取SKU分页列表
    @ApiOperation("获取SKU分页列表")
    @GetMapping("list/{page}/{limit}")
    public Result list(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit
    ){
        IPage<SkuInfo> skuInfoIPage = new Page<>(page, limit);
        IPage<SkuInfo> p = manageService.list(skuInfoIPage);
        return Result.ok(p);
    }

    // 根据spuId获取图片列表
    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId) {
        List<SpuImage> spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }


    // 根据spuId查询所有此商品的销售属性及属性值
    @ApiOperation("根据spuId查询所有此商品的销售属性及属性值")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    // 保存SKU数据
    @ApiOperation("保存SKU数据")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    // 上架
    @ApiOperation("上架")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId) {
        manageService.onSale(skuId);
        return Result.ok();
    }

    // 下架
    @ApiOperation("下架")
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }
}
